package com.example.deliveryapplication

import android.app.AlertDialog
import android.content.Context
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.viewModelScope
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.text.SimpleDateFormat
import java.util.*

object MyUtils {
}


/**
 * 处理null字符串
 */
fun Any?.handlerNull(): String {
    val data = this.toString()
    return if (data == "null" || data.isEmpty()) {
        ""
    } else {
        data.trim()
    }
}

fun Context.showToast(msg: String) {
    Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
}

fun getNewUUid(): String {
    return UUID.randomUUID().toString().replace("-", "")
}

/***ViewModel携程 处理错误*/
fun BaseViewModel.launch(block: suspend () -> Unit) = viewModelScope.launch {
    try {
        withContext(Dispatchers.IO) {
            block()
        }
    } catch (e: Exception) {
        // Handle exception
    }
}

fun handlerCurrentTimeToStandardString(s: Long): String {
    val res: String
    val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
    val lt: Long = s
    val date = Date(lt)
    res = simpleDateFormat.format(date)
    return res
}

fun RecyclerView.Adapter<RecyclerView.ViewHolder>.removeAt(position: Int, size: Int) {

    notifyItemRemoved(position)
    notifyItemRangeChanged(position, size - position)
}
fun formatTime(lt:Long,pattern:String):String{
    val res: String
    val simpleDateFormat = SimpleDateFormat(pattern, Locale.CHINA)
    val date = Date(lt)
    res = simpleDateFormat.format(date)
    return res
}
/***通用Dialog*/
fun Context.commonDialog(msg: String, block: () -> Unit) {
    AlertDialog.Builder(this).setTitle(msg)
        .setPositiveButton("确认") { _, _ ->
            block()
        }.setNegativeButton("返回") { _, _ ->
        }.show()
}